using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class ProcTrees2D : MonoBehaviour
{
    public List<GameObject> seeds;
    public List<GameObject> crowns;
    public bool runtime = false;
    public float genSpeed = 1.0f;

    private List<Leaf2D> leaves;
    private List<Branch2D> branches;
    private bool finished;

    void Start()
    {
        leaves = new List<Leaf2D>();
        branches = new List<Branch2D>();
        finished = false;
    }

    public void Generate()
    {
        foreach (var seed in seeds)
        {
            // Seed grows to all possible nodes in all crowns
            if (!seed.GetComponent<Seed>().attachCrowns)
            {
                
            }
        }
    }

    public void Grow(float growDist, float maxDist, float minDist)
    {
        if (!finished)
        {
            for (int i = 0; i < leaves.Count; i++)
            {
                Leaf2D leaf = leaves[i];
                float lowestDist = maxDist;
                Vector3 lowestDir = Vector3.zero;
                Branch2D closestBranch = null;
                for (int j = 0; j < branches.Count; j++)
                {
                    Branch2D branch = branches[j];
                    Vector3 dir = leaf.location - branch.location;
                    float dist = Vector3.Distance(leaf.location, branch.location);
                    if (dist > maxDist)
                    {
                        continue;
                    }
                    if (dist < minDist)
                    {
                        leaves.Remove(leaf);
                    }
                    if (closestBranch == null || dist <= lowestDist)
                    {
                        closestBranch = branch;
                    }
                    if (lowestDir == Vector3.zero || dist <= lowestDist)
                    {
                        lowestDir = dir;
                    }
                    if (dist <= lowestDist)
                    {
                        lowestDist = dist;
                    }
                }
                // We have the closest branch to that leaf
                if (closestBranch != null)
                {
                    closestBranch.growCount++;
                    lowestDir.Normalize();
                    closestBranch.growDir += lowestDir;
                }
            }

            finished = true;
            for (int i = branches.Count - 1; i > 0; i--)
            {
                Branch2D branch = branches[i];
                branch.Draw(thickness);
                if (branch.growCount > 0)
                {
                    finished = false;
                    branch.growDir /= branch.growCount;
                    branch.growDir.Normalize();
                    branch.growDir *= growDist;
                    Branch2D newBranch = new Branch2D(branch.location + branch.growDir, branch, treeObject.transform, growDist, meshSubdivisions);
                    branches.Add(newBranch);

                    branch.growDir = new Vector3(0.0f, 0.0f, 0.0f);
                    branch.growCount = 0;
                    branch.area = 0.0f;
                }
            }
            if (!finished)
                Grow(growDist, maxDist, minDist);
            else
                RenderTree();
        }
    }

    public void RenderTree()
    {

    }
}
